{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 基本类型和计算\n",
    "\n",
    "## 注释的用法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n",
      "hello world\n"
     ]
    }
   ],
   "source": [
    "# 单行注释\n",
    "print(\"hello world\")  # 行末注释\n",
    "\n",
    "\"\"\"\n",
    "多行注释\n",
    "\"\"\"\n",
    "print(\"hello world\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数字, num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3000 <class 'int'>\n"
     ]
    }
   ],
   "source": [
    "stock_num = 3000 # 3000 只股票在交易\n",
    "print(stock_num, type(stock_num))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20.18 <class 'float'>\n"
     ]
    }
   ],
   "source": [
    "stock_price = 20.18\n",
    "print(stock_price, type(stock_price))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.18 <class 'float'>\n"
     ]
    }
   ],
   "source": [
    "stock_price = 0.18\n",
    "print(stock_price, type(stock_price))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-20 <class 'int'>\n"
     ]
    }
   ],
   "source": [
    "stock_pe = -20\n",
    "print(stock_pe, type(stock_pe))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "62300000000.0 <class 'float'>\n"
     ]
    }
   ],
   "source": [
    "stock_value = 6.23E10\n",
    "print(stock_value, type(stock_value))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字符串，string"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "万科 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "stock_name = '万科'\n",
    "print(stock_name, type(stock_name))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sz000111 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "stock_code = 'sz000111'\n",
    "print(stock_code, type(stock_code))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 布尔值，只有2个，True和False，大小写敏感"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True False <class 'bool'>\n"
     ]
    }
   ],
   "source": [
    "print(True, False, type(True))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 空值：None，大小写敏感，表示没有值的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None <class 'NoneType'>\n"
     ]
    }
   ],
   "source": [
    "print(None, type(None))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 变量，名称一定要有意义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123\n"
     ]
    }
   ],
   "source": [
    "xx_value = 123\n",
    "yy_number = 123\n",
    "print(xx_value)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 算数符号，+ - * / %(余数) //(取整)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "print(7 // 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 同类型变量进行运算的时候，结果还是该类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0\n"
     ]
    }
   ],
   "source": [
    "print(5.0 // 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 自运算的快速写法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3600\n"
     ]
    }
   ],
   "source": [
    "stock_num = 3000\n",
    "# stock_num = stock_num + 600\n",
    "stock_num += 600\n",
    "print(stock_num)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 比较运算 > < >= <= == !="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "num1 = 15\n",
    "num2 = 20\n",
    "print(num1!=num2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 布尔运算 and &    or  |(python3是否改动？)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(2>1 & 2 != 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(2>1 or 2 == 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 内置数据结构"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# list 介绍\n",
    "使用[]中括号新建的一个数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "list_var = []\n",
    "print(list_var, type(list_var))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, '2', 3, 4.0, 5, 6, 'seven', [8], '九']\n"
     ]
    }
   ],
   "source": [
    "list_var = [1, \"2\", 3, 4.0, 5, 6, \"seven\", [8], \"九\"]\n",
    "print(list_var)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## list 常见操作：索引，选取list中的某个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "九\n",
      "<class 'list'>\n",
      "9\n",
      "[1, '2', 3, 100, 5, 6, 'seven', [8], '九']\n"
     ]
    }
   ],
   "source": [
    "print(list_var[8])\n",
    "print(type(list_var[-2]))\n",
    "print(len(list_var))\n",
    "list_var[3] = 100\n",
    "print(list_var)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, '2', 3, 4.0, 5, 6, 'seven', [8], '九']\n"
     ]
    }
   ],
   "source": [
    "list_var = [1, \"2\", 3, 4.0, 5, 6, \"seven\", [8], \"九\"]\n",
    "print(list_var)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4.0, 5, 6, 'seven', [8]]\n"
     ]
    }
   ],
   "source": [
    "print(list_var[3:8])  # 左闭右开 [3:8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, '2', 3, 4.0]\n"
     ]
    }
   ],
   "source": [
    "print(list_var[:4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5, 6, 'seven', [8], '九']\n"
     ]
    }
   ],
   "source": [
    "print(list_var[4:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['2', 5]\n"
     ]
    }
   ],
   "source": [
    "print(list_var[1:7:3]) # 在1-7的区间内（左闭右开），每3个元素选一个"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## list相加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, '2', 3, 4.0, 5, 6, 'seven', [8], '九']\n"
     ]
    }
   ],
   "source": [
    "list_var1 = [1, '2', 3, 4.0, 5]\n",
    "list_var2 = [ 6, 'seven', [8], '九']\n",
    "print(list_var1 + list_var2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 判断一个元素是否在list中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(1 in list_var1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## len, max, min"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "0\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "list_var = [1, 2, 3, 4, 5]\n",
    "print(len(list_var))\n",
    "print(len([]))\n",
    "print(min(list_var))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 删除一个元素\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "del list_var[2]\n",
    "print(list_var)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 添加一个元素在后面"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 4, 5, 6]\n"
     ]
    }
   ],
   "source": [
    "list_var.append(6)\n",
    "print(list_var)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 4, 5, [8, 9, 12], [8, 9, 12], [8, 9, 12]]\n"
     ]
    }
   ],
   "source": [
    "list_var.append([8, 9, 12])\n",
    "print(list_var)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 两个list合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 4, 5, 6, 8, 9, 12, 18, 9, 12]\n"
     ]
    }
   ],
   "source": [
    "list_var.extend([18, 9, 12])\n",
    "print(list_var)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 逆序、排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[12, 9, 18, 12, 9, 8, 6, 5, 4, 2, 1]\n"
     ]
    }
   ],
   "source": [
    "list_var.reverse()\n",
    "print(list_var)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 4, 5, 6, 8, 9, 9, 12, 12, 18]\n"
     ]
    }
   ],
   "source": [
    "list_var.sort()\n",
    "print(list_var)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## range函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "print(list(range(5)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# dict 介绍\n",
    "使用{}大括号新建的一个dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "dict_var = {}\n",
    "print(dict_var, type(dict_var))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'sh600000': '浦发银行', 'sz000002': '万科A', 300001: '特锐德'}\n"
     ]
    }
   ],
   "source": [
    "dict_var = {'sh600000': \"浦发银行\",\n",
    "    'sz000002': '万科A',\n",
    "    300001: '特锐德'\n",
    "}\n",
    "print(dict_var)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 取值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "浦发银行\n"
     ]
    }
   ],
   "source": [
    "print(dict_var['sh600000'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "浦发银行\n"
     ]
    }
   ],
   "source": [
    "print(dict_var.get('sh600000'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 增加，修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'sh600000': '浦发银行', 'sz000002': '万科A', 300001: '特锐德', 'sh000001': '上证综合指数'}\n"
     ]
    }
   ],
   "source": [
    "dict_var['sh000001'] = '上证综合指数'\n",
    "print(dict_var)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 输出一个dict中所有key和value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['sh600000', 'sz000002', 300001, 'sh000001'])\n"
     ]
    }
   ],
   "source": [
    "print(dict_var.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_values(['浦发银行', '万科A', '特锐德', '上证综合指数'])\n"
     ]
    }
   ],
   "source": [
    "print(dict_var.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
